package com.mkb.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.ImmutableMap;
import com.mkb.common.BaseController;
import com.mkb.common.ModuleURL;
import com.mkb.common.WebConstant;
import com.mkb.entity.AreaDTO;
import com.mkb.entity.UserInfoDTO;
import com.mkb.service.*;
import com.zz.common.result.ResultInfo;
import com.zz.common.utils.CookieUtil;
import com.zz.common.utils.JwtHelper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class UserLoginController extends BaseController {
	@Autowired
	private UserInfoService userInfoService;
	@Autowired
	private AreaService areaService;
	@Autowired
	private GraphUserService graphUserService;
	@Autowired
	private GraphClueService graphClueService;
	@Autowired
	private GraphCaseService graphCaseService;

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public ModelAndView login() {
		return new ModelAndView("page/login");
	}

	@RequestMapping(value = "/page/login", method = RequestMethod.GET)
	@ResponseBody
	public String text() {
		return "Hello";
	}

	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public ModelAndView index() {
		return new ModelAndView("index");
	}

	@RequestMapping(value = "/dashboard", method = RequestMethod.GET)
	public ModelAndView dashboard(Model model)throws Exception {
		//查询页面数据
		Map<String, Object> obj = new HashMap<>();
		int userNum = graphUserService.count(obj);
		int clueNum = graphClueService.count(obj);
		int caseNum = graphCaseService.count(obj);
		model.addAttribute("userNum", userNum);
		model.addAttribute("clueNum", clueNum);
		model.addAttribute("caseNum", caseNum);

		//获取每个行政区的 数量 组装json格式
		model.addAttribute("areaJson", getAreaJson());

		return new ModelAndView("dashboard");
	}

	@RequestMapping(value = "", method = RequestMethod.GET)
	public ModelAndView dashboard2(Model model)throws Exception {
		return new ModelAndView("index");
	}

	/**
	 *
	 * @return
	 * @throws Exception
	 */
	private String getAreaJson() throws Exception{
		List<AreaDTO> areaDTOList = areaService.queryList(AreaDTO.builder().build());
		JSONArray jsonArray = new JSONArray();
		Map<String, Object> obj = new HashMap<>();
		for (AreaDTO areaDTO : areaDTOList){
			obj.put("areaId", areaDTO.getId());
			int userNum = graphUserService.count(obj);
			int clueNum = graphClueService.count(obj);
			int caseNum = graphCaseService.count(obj);
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("name", areaDTO.getName());
			jsonObject.put("value", caseNum);
			jsonObject.put("clue", clueNum);
			jsonObject.put("user", userNum);
			jsonArray.add(jsonObject);
		}
		return jsonArray.toJSONString();
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@ResponseBody
	public ResultInfo login2(HttpServletRequest request, HttpServletResponse response, @RequestBody UserInfoDTO userInfoDTO) throws Exception{
		List<UserInfoDTO> userInfoDTOS = userInfoService.queryList(UserInfoDTO.builder().username(userInfoDTO.getUsername()).build());
		if (CollectionUtils.isEmpty(userInfoDTOS) || userInfoDTOS.size() > 1 || !userInfoDTOS.get(0).getPassword().equals(userInfoDTO.getPassword())){
			return ResultInfo.err("用户名或密码不正确！");
		}

		UserInfoDTO entity = userInfoDTOS.get(0);

		//存储session
		request.getSession().setAttribute(WebConstant.SESSION_CONTEXT_NAME, entity);

		//String token = JwtHelper.genToken(ImmutableMap.of("name", entity.getName(), "ts", Instant.now().getEpochSecond() + ""));

		//CookieUtil.setCookieValue(request, response, WebConstant.COOKIE_LASTLOGIN_ADMINID, token, WebConstant.COOKIE_LOGIN_DOMAIN, -1);

		return ResultInfo.ok();
	}

}
